package com.gitee.fsmxm.thinking.in.spring.dependency.lookup;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

/**
 * ObjectProvider 获取bean demo
 */
public class ObjectProviderDemo {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        applicationContext.register(ObjectProviderDemo.class);

        applicationContext.refresh();

        ObjectProvider<String> beanProvider = applicationContext.getBeanProvider(String.class);

        System.out.println(beanProvider.getObject());

        beanProvider.stream().forEach(System.out::println);

        applicationContext.close();

    }

    @Bean
    @Primary
    public String helloWorld() {
        return "hello, world";
    }

    @Bean
    public String message() {
        return "hello, message";
    }

}
